<!--
 * @Author zhaoyuemi
 * @Date 2020/10/13
 * @LastEditTime 2020/10/19
 * @LastEditors 86157
 * @Description detail 详情
 * @FilePath
 * @ router
 * @version 1.0
-->

<template>
    <div class="index">
        <div class="container">
            <!--轮播图-->
            <div class="swiper-box">
                <div class="nav-menu">
                    <ul class="menu-warp">
                        <li class="menu-item">
                            <a href="javascript:;">手机 电话卡</a>
                            <div class="children">
                                <ul v-for="(item,index) in menuList" :key="index">
                                    <li v-for="(sub,i) in item" :key="i">
                                        <a :href="'/#/product/'+sub.id">
                                            <img :v-lazy="sub.img" alt="">
                                            {{sub.name}}
                                        </a>
                                    </li>
                                </ul>
                            </div>
                        </li>
                        <li class="menu-item">
                            <a href="javascript:;">电视 盒子</a>
                            <div class="children"></div>
                        </li>
                        <li class="menu-item">
                            <a href="javascript:;">笔记本 平板</a>
                            <div class="children"></div>
                        </li>
                        <li class="menu-item">
                            <a href="javascript:;">家电 插线板</a>
                            <div class="children"></div>
                        </li>
                        <li class="menu-item">
                            <a href="javascript:;">出行 穿戴</a>
                            <div class="children"></div>
                        </li>
                        <li class="menu-item">
                            <a href="javascript:;">智能 路由器</a>
                            <div class="children"></div>
                        </li>
                        <li class="menu-item">
                            <a href="javascript:;">电源 配件</a>
                            <div class="children"></div>
                        </li>
                        <li class="menu-item">
                            <a href="javascriptjavascript:;">生活 箱包</a>
                            <div class="children"></div>
                        </li>

                    </ul>
                </div>
                <swiper :options="swiperOption">
                    <swiper-slide v-for="(item,index) in sliderListL" :key="index">
                        <a :href="'/#/product/'+item.id">
                            <img v-lazy="item.img" alt="">
                        </a>
                    </swiper-slide>
                    <div class="swiper-pagination" slot="pagination"></div>
                    <div class="swiper-button-prev" slot="button-prev"></div>
                    <div class="swiper-button-next" slot="button-next"></div>
                </swiper>
            </div>
            <!--广告条-->
            <div class="ads-box">
                <a :href="'/#/product/'+item.id" v-for="(item,index) in adsList" v-bind:key="index">
                    <img v-lazy="item.img" alt="">
                </a>
            </div>
            <!--广告条-->
            <div class="bannar">
                <a :href="'/#/product/30'">
                    <img width="100%" v-lazy="'/imgs/banner-1.png'" alt="">
                </a>

            </div>

        </div>
        <!--商品内容区域-->
        <div class="product-box">
            <div class="container">
                <h2>手机</h2>
                <div class="wrapper">
                    <div class="bannar-left">
                        <a :href="'/#/product/35'">
                            <img v-lazy="'/imgs/mix-alpha.jpg'" alt="">
                        </a>
                    </div>
                    <div class="list-box">
                        <div class="list" v-for="(arr,index) in phoneList" v-bind:key="index">
                            <div class="item"  v-for="(item,i) in arr" v-bind:key="i">
                                <span :class="i%2==0 ? 'new-pro':'kill-pro'">{{i%2==0 ? '新品':'秒杀'}}</span>
                                <div class="item-img">
                                    <img v-lazy="item.mainImage" alt="">
                                </div>
                                <div class="item-info">
                                    <h3>{{item.name}}</h3>
                                    <p>{{item.subtitle}}</p>
                                    <p class="price" @click="addCart(item.id)">{{item.price}}元</p>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>

        </div>
        <service-bar></service-bar>
        <modal title="提示"
               sureText="查看购物车"
               btnType="1"
               modal-type="middle"
               :showModal="showModal"
               @submit="goToCart"
               @cancle="showModal=false"
        >
            <template v-slot:body>
                <p>商品添加成功！</p>
            </template>
        </modal>
    </div>
</template>

<script>
    import 'swiper/dist/css/swiper.css';
    import {swiper, swiperSlide} from 'vue-awesome-swiper';
    import ServiceBar from "../components/ServiceBar";
    import Modal from "../components/Modal";

    export default {
        name: "homeIndex",
        components: {
            Modal,
            swiper,
            swiperSlide,
            ServiceBar
        },
        data() {
            return {
                swiperOption: {
                    pagination: {
                        el: '.swiper-pagination',
                        clickable: true,
                    },
                    autoplay: true,
                    loop: true,
                    effect: 'cube',
                    cube: {
                        slideShadows: true,
                        shadow: true,
                        shadowOffset: 100,
                        shadowScale: 0.6
                    },
                    navigation: {
                        nextEl: '.swiper-button-next',
                        prevEl: '.swiper-button-prev'
                    }

                },
                sliderListL: [
                    {id: '42', img: '/imgs/slider/slide-1.jpg'},
                    {id: '45', img: '/imgs/slider/slide-2.jpg'},
                    {id: '46', img: '/imgs/slider/slide-3.jpg'},
                    {id: '42', img: '/imgs/slider/slide-4.jpg'},
                    {id: '42', img: '/imgs/slider/slide-5.jpg'}
                ],
                menuList: [
                    [
                        {
                            id: '30',
                            img: '/imgs/item-box-1.png',
                            name: '小米CC9',
                        },
                        {
                            id: '31',
                            img: '/imgs/item-box-2.png',
                            name: '小米8青春版',
                        },
                        {
                            id: '32',
                            img: '/imgs/item-box-3.jpg',
                            name: 'Redmi K20 Pro',
                        },
                        {
                            id: '33',
                            img: '/imgs/item-box-4.jpg',
                            name: '移动4G专区',
                        },
                    ],
                    [
                        {
                            id: '30',
                            img: '/imgs/item-box-1.png',
                            name: '小米CC9',
                        }, {
                            id: '30',
                            img: '/imgs/item-box-1.png',
                            name: '小米CC9',
                        }, {
                            id: '30',
                            img: '/imgs/item-box-1.png',
                            name: '小米CC9',
                        }, {
                            id: '30',
                            img: '/imgs/item-box-1.png',
                            name: '小米CC9',
                        },
                    ],
                    [
                        {
                            id: '31',
                            img: '/imgs/item-box-2.png',
                            name: '小米8青春版',
                        }, {
                            id: '31',
                            img: '/imgs/item-box-2.png',
                            name: '小米8青春版',
                        }, {
                            id: '31',
                            img: '/imgs/item-box-2.png',
                            name: '小米8青春版',
                        }, {
                            id: '31',
                            img: '/imgs/item-box-2.png',
                            name: '小米8青春版',
                        },
                    ],
                    [
                        {
                            id: '32',
                            img: '/imgs/item-box-3.jpg',
                            name: 'Redmi K20 Pro',
                        }, {
                            id: '32',
                            img: '/imgs/item-box-3.jpg',
                            name: 'Redmi K20 Pro',
                        }, {
                            id: '32',
                            img: '/imgs/item-box-3.jpg',
                            name: 'Redmi K20 Pro',
                        }, {
                            id: '32',
                            img: '/imgs/item-box-3.jpg',
                            name: 'Redmi K20 Pro',
                        },
                    ],
                    [
                        {
                            id: '33',
                            img: '/imgs/item-box-4.jpg',
                            name: '移动4G专区',
                        }, {
                            id: '33',
                            img: '/imgs/item-box-4.jpg',
                            name: '移动4G专区',
                        }, {
                            id: '33',
                            img: '/imgs/item-box-4.jpg',
                            name: '移动4G专区',
                        }, {
                            id: '33',
                            img: '/imgs/item-box-4.jpg',
                            name: '移动4G专区',
                        },
                    ],
                    [
                        {
                            id: '30',
                            img: '/imgs/item-box-1.png',
                            name: '小米CC9',
                        },
                        {
                            id: '31',
                            img: '/imgs/item-box-2.png',
                            name: '小米8青春版',
                        },
                        {
                            id: '32',
                            img: '/imgs/item-box-3.jpg',
                            name: 'Redmi K20 Pro',
                        },
                        {
                            id: '33',
                            img: '/imgs/item-box-4.jpg',
                            name: '移动4G专区',
                        },
                    ],

                ],
                adsList:[
                    {
                        id:'33',
                        img:'/imgs/ads/ads-1.png'
                    },
                    {
                        id:'48',
                        img:'/imgs/ads/ads-2.jpg'
                    },
                    {
                        id:'45',
                        img:'/imgs/ads/ads-3.png'
                    },
                    {
                        id:'47',
                        img:'/imgs/ads/ads-4.jpg'
                    },
                ],
                phoneList:[
                    [1,2,3,4],
                    [1,2,3,4],
                ],
                showModal:false,
            }
        },
        methods:{
            init(){
                this.axios.get(
                    '/api/products',{
                        params:{
                            categoryId:'100012',
                            pageSize:8
                        }
                    }
                ).then((res)=>{
                    console.log(res);
                    this.phoneList = [res.list.slice(0,4),res.list.slice(4,8)]

                })
            },
            //加购物车
            addCart(id){
                this.axios.post(
                    '/api/carts',{
                        productId:id,
                        selected:true
                    }
                ).then((res)=>{
                    // console.log(res);
                    this.showModal = true;
                    this.$store.dispatch('saveCartCount',res.cartTotalQuantity);
                }).catch(()=>{
                    // this.showModal = true;
                })
            },
            goToCart(){
                this.$router.push('/cart')
            }

        },
        mounted() {
            this.init();
        }
    }
</script>

<style scoped lang="scss">
    @import "./../assets/scss/mixin";
    @import "./../assets/scss/config";

    .index {
        .swiper-box {
            position: relative;

            .nav-menu {
                position: absolute;
                left: 0;
                z-index: 9;
                width: 264px;
                height: 451px;
                padding: 26px 0;
                background: #55585a7a;
                box-sizing: border-box;

                .menu-warp {
                    .menu-item {
                        height: 50px;
                        line-height: 50px;
                        font-size: 16px;

                        a {
                            position: relative;
                            display: block;
                            font-size: 16px;
                            color: #ffffff;
                            padding-left: 30px;

                            &:after {
                                position: absolute;
                                top: 17px;
                                right: 30px;
                                content: ' ';
                                @include bgImg(10px, 15px, '/imgs/icon-arrow.png')
                            }
                        }


                        .children{
                            display: none;
                            position: absolute;
                            left: 264px;
                            top: 0;
                            width: 962px;
                            height: 451px;
                            background: $colorG;
                            border: 1px solid $colorH;
                            ul{
                                display: flex;
                                justify-content: space-between;
                                height: 75px;
                                li{
                                    flex: 1;
                                    height: 75px;
                                    line-height: 75px;
                                    padding-left: 23px;
                                    img{
                                        vertical-align: middle;
                                        width: 42px;
                                        height: 35px;
                                        margin-right: 15px;
                                    }
                                    a{
                                        font-size: 14px;
                                        color: $colorB;
                                    }
                                }
                            }
                        }
                        &:hover {
                            background: $colorA;
                            .children{
                                display: block;
                            }
                        }
                    }

                }
            }

            .swiper-container {
                height: 451px;

                .swiper-button-prev {
                    left: 274px;
                }

                img {
                    width: 100%;
                    height: 100%;
                }
            }
        }
        .ads-box{
            @include flex();
            margin: 14px 0 31px 0;
            a{
                width: 296px;
                height: 167px;
            }
        }
        .bannar {
            margin-bottom: 50px;
        }
        .product-box{
            background: $colorJ;
            padding: 30px 0 50px 0;
            h2{
                font-size: $fontF;
                height: 21px;
                line-height: 21px;
                color: $colorB;
                margin-bottom: 20px;
            }
            .wrapper{
                display: flex;
                .bannar-left{
                    margin-right: 16px;
                    img{
                        width: 224px;
                        height: 619px;
                    }
                }
                .list-box{
                    .list{
                        @include flex();
                        width: 986px;
                        margin-bottom: 14px;
                        &:last-child{
                            margin-bottom: 0;
                        }
                        .item{
                            width: 236px;
                            height: 302px;
                            background: $colorG;
                            text-align: center;
                            span{
                                display: inline-block;
                                width: 67px;
                                height: 24px;
                                line-height: 24px;
                                color: $colorG;
                                &.new-pro{
                                    background: #7ecf68 ;
                                }
                                &.kill-pro{
                                    background: #e82626 ;
                                }
                            }
                            .item-img{
                                display: flex;
                                justify-content: center;
                                align-items: center;
                                height: 195px;
                                img{
                                    /*width: 160px;*/
                                    height: 160px;
                                }
                            }
                            .item-info{
                                h3{
                                    line-height: $fontJ;
                                    font-size: $fontJ;
                                    color: $colorB;
                                    font-weight: bold;

                                }
                                p{
                                    margin: 6px auto 13px;
                                    line-height: 13px;
                                    color: $colorD;
                                }
                                .price{
                                    color: #f20a0a;
                                    font-size: $fontJ;
                                    font-weight: bold;
                                    cursor: pointer;
                                    &:after{
                                        content: ' ';
                                        vertical-align: middle;
                                        margin-left: 5px;
                                        @include bgImg(22px,22px,'/imgs/icon-cart-hover.png')
                                    }
                                }
                            }
                        }

                    }
                }
            }
        }

    }


</style>